package com.yzy.fly.entity.role;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yzy.fly.annotation.OrderSeq;
import com.yzy.fly.kernel.model.FlyBasicModel;
import com.yzy.fly.kernel.model.ability.order.Order;
import com.yzy.fly.util.DateUtil;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;

@Data
@TableName(value = "fly_interface", autoResultMap = true)
public class FlyInterface extends FlyBasicModel<Integer,FlyInterface> implements Order<Integer>{

    @TableId(type = IdType.AUTO)
    private Integer id;
    @OrderSeq
    private Integer seq;
    private String label;
    private String name;
    private String path;
    private String type;
//    @ParentId
    private Integer parentId;
    private Integer componentId;
    private Integer processId;
    private Integer systemId;

    private String descr;

    @TableField(value = "params",typeHandler = FastjsonTypeHandler.class)
    private JSONArray params = new JSONArray();
    @TableField(value = "result",typeHandler = FastjsonTypeHandler.class)
    private JSONArray result = new JSONArray();
    @TableField(exist=false)
    private List<FlyRequestMapping> requestMappings;

    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss )
    private Date createTime;
    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss )
    private Date updateTime;
    @JSONField(serialize= false, deserialize = false)
    public Integer getOrderId(){
        return id;
    }
    @JSONField(serialize= false, deserialize = false)
    public Integer getOrderParentId(){
        return componentId;
    }
    @JSONField(serialize= false, deserialize = false)
    public Integer getOrderValue(){
        return seq;
    }
    @JSONField(serialize= false, deserialize = false)
    public String getOrderTableName(){
        return "fly_interface";
    }
    @JSONField(serialize= false, deserialize = false)
    public String getOrderSqlUpById(){
        String sql = "update "+ getOrderTableName() +" a set seq = seq - 1  where id = "+ getOrderId() +" and seq = "+ getOrderValue() + "";
        return sql;
    }
    @JSONField(serialize= false, deserialize = false)
    public String getOrderSqlDownById(){
        String sql = "update "+ getOrderTableName() +" a set seq = seq + 1  where id = "+ getOrderId() +" and seq = "+ getOrderValue() + "";
        return sql;
    }
    @JSONField(serialize= false, deserialize = false)
    public String getOrderSqlUpByParentId(){
        String sql = getOrderSqlUpByParentId(getOrderValue() + 1);
        return sql;
    }
    @JSONField(serialize= false, deserialize = false)
    public String getOrderSqlDownByParentId(){
        String sql = getOrderSqlDownByParentId(getOrderValue() - 1);
        return sql;
    }
    @JSONField(serialize= false, deserialize = false)
    public String getOrderSqlUpByParentId(int seq){
        String sql = "update "+ getOrderTableName() +" a set seq = seq - 1  where componentId = "+ getOrderParentId() +" and seq = "+ seq;
        return sql;
    }
    @JSONField(serialize= false, deserialize = false)
    public String getOrderSqlDownByParentId(int seq){
        String sql = "update "+ getOrderTableName() +" a set seq = seq + 1  where componentId = "+ getOrderParentId() +" and seq = "+ seq;
        return sql;
    }

    @JSONField(serialize= false, deserialize = false)
    @Override
    public String getOrderSqlMaxSeqNo() {
        String sql = "select max(a.seq) from "+ getOrderTableName() +" a where componentId = "+ getOrderParentId();
        return sql;
    }

}
